/**
 * Created by ebalmes Oct 6, 2009 8:38:04 AM
 * 
 */
package gui.dialogs;

import java.awt.BorderLayout;
import java.util.logging.Level;
import java.util.logging.Logger;

import exceptions.DataException;
import gui.MainFrame;
import gui.actions.ActionFactory;
import gui.actions.XAction;
import gui.dialogs.panels.SearchPatientPanel;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.DataModel;
import model.table.SearchPatientTableModel;

public class OpenDialog extends BasicDialog
{
  private int tajNumber;
  private SearchPatientTableModel model;
  private static Logger logger = Logger.getLogger(OpenDialog.class.getName());
  
  public OpenDialog(String title, JFrame owner) {
    super(title, owner);
    logger.log(Level.INFO, "Open patient dialog created.");
  }

  @Override
  public void createGUI() {
    model = new SearchPatientTableModel();
    mainPanel = new SearchPatientPanel(model);  
    controlPanel = createControlPanel(this);
    
    setLayout(new BorderLayout());
    add(mainPanel, BorderLayout.CENTER);
    add(controlPanel, BorderLayout.SOUTH);
  }

  @Override
  public void loadContent() {
    model.searchFieldChanged(""); 
  }

  @Override
  public void releaseResources() {
    model.clearTableModel();
  }

  @Override
  public void OK() {
    try {
      DataModel.getInstance().setSelectedPatient(tajNumber);
    } catch (DataException e) {
      logger.log(Level.SEVERE, "The patient cannot be selected.");
      JOptionPane.showMessageDialog(this, "The patient cannot be selected: " + e);
    }
    logger.log(Level.INFO, "Patient (" + tajNumber + ") selected.");
  }
}
